package com.qf.web.sys_menu.servie.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.web.sys_menu.entity.Menu;
import com.qf.web.sys_menu.entity.MenuTree;
import com.qf.web.sys_menu.mapper.MenuMapper;
import com.qf.web.sys_menu.servie.MenuService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author xufuhan
 * @Date 2024/9/10 20:32
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Override
    public List<Menu> getMenuList() {

        QueryWrapper<Menu> wrapper=new QueryWrapper<>();
        wrapper.lambda().orderByAsc(Menu::getOrderNum);
        List<Menu> menus = this.baseMapper.selectList(wrapper);
        //组装树
        return MenuTree.makeManuTree(menus, 0L);
    }

    @Override
    public List<Menu> getParentList() {
        //查询菜单类型
        String[] types={"0","1"};
        QueryWrapper<Menu> wrapper=new QueryWrapper<>();
        wrapper.lambda().in(Menu::getType, Arrays.asList(types)).orderByAsc(Menu::getOrderNum);
        List<Menu> menus = this.baseMapper.selectList(wrapper);
        //构造一个顶级菜单
        Menu menu = new Menu();
        menu.setMenuId(0L);
        menu.setTitle("顶级菜单");
        menu.setParentId(-1L);
        menus.add(menu);
        //组装树
        List<Menu> menuList = MenuTree.makeManuTree(menus, -1L);
        return menuList;
    }

    @Override
    public List<Menu> getMenuByUserId(Long userId) {
        return this.baseMapper.getMenuByUserId(userId);
    }

    @Override
    public List<Menu> getMenuByRoleId(Long roleId) {
        return this.baseMapper.getMenuByRoleId(roleId);
    }
}
